package com.example.kotlinproject.modules.login

import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.SparseArray
import android.view.View
import android.widget.Toast
import androidx.core.util.forEach
import com.example.kotlinproject.R
import com.example.kotlinproject.api.WanAndroidAPI
import com.example.kotlinproject.base.BaseActivity
import com.example.kotlinproject.entity.LoginResponse
import com.example.kotlinproject.modules.login.inter.LoginPresenter
import com.example.kotlinproject.modules.login.inter.LoginView
import com.example.kotlinproject.net.APIClient
import com.example.kotlinproject.net.ApiResponse
import com.xiangxue.kotlinproject.config.Flag
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*

class LoginActivity : BaseActivity<LoginPresentImpl>(),LoginView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //val instanceRetrofit = APIClient.instance.instanceRetrofit(WanAndroidAPI::class.java)
        bt_login.setOnClickListener(onClickLister)

    }



    private val onClickLister = View.OnClickListener { view ->
        when(view.id){
            R.id.bt_login ->{
                val user = et_user.text.toString()
                val password = et_password.text.toString()
               Log.d(Flag.TAG, "user:$user,password:$password")

                presenter.login(this,user,password)
                    /*
                    *设置匿名内部类,语法object:表示是个内部类,实例化的时候不需要加()
                    * Consumer是简易话的observer,只有一个accept的回调,只接收到observer的accept方法
                    */
                  /*  .subscribe(object :Consumer<LoginResponseWrapper<LoginResponse>>{
                        override fun accept(t: LoginResponseWrapper<LoginResponse>?) {

                        }
                    })*/

            }
        }
    }

    override fun createP(): LoginPresentImpl {
        return LoginPresentImpl(this@LoginActivity)
    }

    override fun recycle() {
        presenter.unAttachView()
    }


    override fun loginSuccess(loginBean: LoginResponse?) {
        Toast.makeText(this@LoginActivity,"登录成功", Toast.LENGTH_LONG).show()
    }

    override fun loginFailure(errorMsg: String?) {
        Log.d("myInterceptor",errorMsg!!)
        Toast.makeText(this@LoginActivity,"登录失败,$errorMsg", Toast.LENGTH_LONG).show()
    }


}
